package com.zhongfeng.api.modules.front.controller;

import com.zhongfeng.api.modules.end.vo.EndVo;
import com.zhongfeng.api.modules.end.vo.params.EndParams;
import com.zhongfeng.api.modules.front.service.FrontService;
import com.zhongfeng.api.modules.front.vo.FrontVo;
import com.zhongfeng.api.modules.front.vo.NumberVo;
import com.zhongfeng.api.modules.front.vo.params.FrontParams;
import com.zhongfeng.api.modules.utils.pojo.PageDataInfo;
import com.zhongfeng.api.modules.utils.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author blueCat
 * @date 2023/2/2 15:45
 * @motto life is a gift
 */
@RestController
@RequestMapping("/api/front")
public class FrontController {

    @Autowired
    private FrontService frontService;

    /**
     * 查询上游信息
     *
     * @param params
     * @return
     */
    @PostMapping("/queryAllFront")
    public Result queryAllFront(@RequestBody FrontParams params) {
        return frontService.queryAllFront(params);
    }

    /**
     * 新增上游信息
     *
     * @param params
     * @return
     */
    @PostMapping("/addFront")
    public Result addFront(@RequestBody FrontParams params) {
        return frontService.addFront(params);
    }

    /**
     * 删除上游信息
     *
     * @param params
     * @return
     */
    @PostMapping("/deleteFront")
    public Result deleteFront(@RequestBody FrontParams params) {
        return frontService.deleteFront(params.getId());
    }

    /**
     * 修改上游信息
     *
     * @param params
     * @return
     */
    @PostMapping("/updateFront")
    public Result updateFront(@RequestBody FrontParams params) {
        return frontService.updateFront(params);
    }

    /**
     * 查询全部A路号码
     *
     * @param params
     * @return
     */
    @PostMapping("/queryAllNumber")
    public Result queryAllNumber(@RequestBody FrontParams params) {
        return frontService.queryAllNumber(params);
    }

    /**
     * 查询上游属性
     *
     * @return
     */
    @PostMapping("/queryFrontProperty")
    public Result queryFrontProperty() {
        return frontService.queryFrontProperty();
    }

    /**
     * 查询落地分布
     *
     * @return
     */
    @PostMapping("/queryFrontDistribution")
    public Result queryFrontDistribution(@RequestBody FrontParams params){
        return frontService.queryFrontDistribution(params);
    }

    /**
     * 下载号码
     *
     * @param resp
     * @throws IOException
     */
    @GetMapping( "/download")
    public void downloadFile(@RequestParam("frontIdList") String listStr,@RequestParam("number") String number,@RequestParam("numbers") String numbers,@RequestParam("endId") String endId,@RequestParam("numberType") Integer numberType,HttpServletResponse resp) throws IOException {
        //得到frontIdList
        List<Integer> frontIdList = new ArrayList<>();
        if (listStr != null && listStr.length() > 0){
            String[] split = listStr.split(",");
            for (String s : split) {
                frontIdList.add(Integer.parseInt(s));
            }
        }
        //设置参数
        FrontParams params = new FrontParams();
        params.setNumber(number);
        params.setFrontIdList(frontIdList);
        params.setNumberType(numberType);
        if (endId != null && endId != ""){
            params.setEndId(Integer.parseInt(endId));
        }
        if (numbers != null && numbers != ""){
            params.setDownloadCounts(Integer.parseInt(numbers));
        }
        frontService.downloadFile(params,resp);
    }

    /**
     * 对接修改号码
     *
     * @return
     */
    @PostMapping("/updateNumber")
    public Result updateNumber(@RequestBody FrontParams params){
        return frontService.updateNumber(params);
    }

    /**
     * 对接自动分配号码
     *
     * @return
     */
    @PostMapping("/autoUpdateNumber")
    public Result autoUpdateNumber(@RequestBody FrontParams params){
        return frontService.autoUpdateNumber(params);
    }

    /**
     * 查询全部落地分组
     * @return
     */
    @PostMapping("/queryFrontGroup")
    public List<Integer> queryFrontGroup(){
        return frontService.queryFrontGroup();
    }

    /**
     * 导入A号码
     * @param params
     * @return
     */
    @PostMapping("/importNumber")
    public Result importNumber(@RequestBody FrontParams params){
        return frontService.importNumber(params);
    }

    /**
     * 新增A号码（新增并发）
     * @param params
     * @return
     */
    @PostMapping("/addNumber")
    public Result addNumber(@RequestBody FrontParams params){
        return frontService.addNumber(params);
    }
}
